# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: jhkim

import glob
import os
import pandas
import numpy

from absl import app, flags

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

FLAGS = flags.FLAGS


def main(_):
  trading_date = "20190915-20191014"
  for title, out_dir, machine, rerun in [
      #("lm-agg-okex-btc-quarter", "server-01.aliyun-cn-hongkong.okex", "server-01.aliyun-cn-hongkong.okex", True),
      #("lm-agg-okex-btc-quarter", "server-02.aliyun-cn-hongkong.okex", "server-02.aliyun-cn-hongkong.okex", True),
      #("lm-pass-okex-btc-quarter", "strategy-23.aliyun-cn-hongkong", "strategy-23.aliyun-cn-hongkong", True),
      ("lm-agg-bitflyer-fxbtcjpy", "strategy-03.ap-northeast-1", "strategy-03.ap-northeast-1", True
      ),
  ]:
    os.makedirs(out_dir, exist_ok=True)
    if rerun:
      os.system(f"rm {out_dir}/*")
      os.system(
          f"./pyrunner_motionfast coin/strategy/mm/tool/taker_ratio2.py --orderlog_machine={machine} --out_dir={out_dir} --trading_date={trading_date} --symbol=BTC-JPY.PERPETUAL --exchange=Bitflyer --market_type=Futures --use_feed_cache=True --time_range=0-24 --feed_machine=feed-01.ap-northeast-1.aws --cpu=14"
      )

    allfilldf = pandas.concat([
        pandas.read_csv(csvfile) for csvfile in glob.glob(f"{out_dir}/fills_*.csv")
    ]).reset_index(drop=True)
    print(
        numpy.corrcoef(allfilldf['filled_timestamp'] - allfilldf['submit_timestamp'],
                       allfilldf['fill_ret_30000ms']))

    fillstatfs = glob.glob(f"{out_dir}/fillstat*.csv")
    df = pandas.concat([pandas.read_csv(fillstatf) for fillstatf in fillstatfs])

    horizs = []
    for horiz, horizdf in df.groupby('horiz', sort=False):
      if float(horiz.split("ret_")[1].replace("ms", "")) < 5000:
        continue
      horizdf = horizdf.sort_values(['trading_date']).reset_index(drop=True)
      dts = pandas.to_datetime(horizdf['trading_date'], format='%Y%m%d')
      # takpnl = ((horizdf['takret'] - 2) * horizdf['takqty']).cumsum()
      # makpnl = ((horizdf['makret'] + 1) * horizdf['makqty']).cumsum()
      takpnl = ((horizdf['takret']) * horizdf['takqty']).cumsum()
      makpnl = ((horizdf['makret']) * horizdf['makqty']).cumsum()
      plt.plot(dts, (takpnl + makpnl), lw=0.5)
      plt.title(f'{title}')
      plt.xticks(rotation=20)
      horizs.append(horiz)
    plt.grid(lw=0.5, which='major')
    plt.grid(lw=0.2, which='minor')
    plt.legend(horizs)
    plt.savefig(f"{out_dir}/fillret_horiz.png")
    plt.close()

    plt.subplot(211)
    plt.plot(dts, horizdf['makqty'].cumsum(), 'r-')
    plt.plot(dts, horizdf['takqty'].cumsum(), 'b-')
    plt.xticks(rotation=10)
    plt.legend(['makqty', 'takqty'])
    plt.subplot(212)
    plt.plot(dts, horizdf['makqty'] / horizdf['takqty'], 'r.')
    plt.xticks(rotation=10)
    plt.tight_layout()
    plt.savefig(f"{out_dir}/fillqty_horiz.png")
    plt.close()

    from matplotlib.backends.backend_pdf import PdfPages
    pp = PdfPages(f'{out_dir}/fillret.pdf')
    for horiz, horizdf in df.groupby('horiz', sort=False):
      horizdf = horizdf.sort_values(['trading_date'])
      # horizdf = horizdf.loc[horizdf['trading_date']!=20190709]
      # horizdf = horizdf.loc[horizdf['trading_date']!=20190710]
      fig = plt.figure()
      ax = fig.add_subplot(1, 1, 1)
      dts = pandas.to_datetime(horizdf['trading_date'], format='%Y%m%d')
      # takpnl = ((horizdf['takret'] - 2) * horizdf['takqty']).cumsum()
      # makpnl = ((horizdf['makret'] + 1) * horizdf['makqty']).cumsum()
      takpnl = ((horizdf['takret'] - 0) * horizdf['takqty']).cumsum()
      makpnl = ((horizdf['makret'] + 0) * horizdf['makqty']).cumsum()
      ax.plot(dts, takpnl, label='takret')
      ax.plot(dts, makpnl, label='makret')
      ax.legend()
      ax.set_title(horiz)
      for tick in ax.get_xticklabels():
        tick.set_rotation(20)
      pp.savefig(fig)
      plt.close(fig)

      fig = plt.figure()
      ax = fig.add_subplot(1, 1, 1)
      dts = pandas.to_datetime(horizdf['trading_date'], format='%Y%m%d')

      # feerat = (horizdf['takqty'] * 2 + horizdf['makqty'] * -1) / (horizdf['takqty'] + horizdf['makqty'])
      feerat = (horizdf['takqty'] * 0 + horizdf['makqty'] * 0) / (horizdf['takqty']
                                                                  + horizdf['makqty'])
      ax.plot(dts, ((horizdf['missm2mret'] - feerat) * horizdf['missqty']).cumsum(),
              label=f'missm2mret, applied fee: {feerat.mean()}')
      ax.plot(dts, ((horizdf['fillm2mret'] - feerat) * horizdf['fillqty']).cumsum(),
              label=f'fillm2mret, applied fee: {feerat.mean()}')
      ax.plot(dts, takpnl + makpnl, label='fillacturet')
      ax.legend()
      ax.set_title(horiz)
      for tick in ax.get_xticklabels():
        tick.set_rotation(20)
      pp.savefig(fig)
      plt.close(fig)
    pp.close()

    plt.close()


if __name__ == '__main__':
  app.run(main)
